package com.lucy.scan.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.lucy.starter.constant.CommonConst;
import com.lucy.starter.domain.ResponseDTO;
import com.lucy.starter.redis.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author kindear
 * 二维码控制器
 */
@RestController
@RequestMapping("system")
@Api(tags = "获取二维码")
@AllArgsConstructor
public class QrCodeController {

    RedisCache redisCache;

    @GetMapping("qrcode")
    @ApiOperation(value = "获取二维码",notes = "")
    public ResponseDTO<Object> getQrCode()  {
        // 简单ID
        String uuid = IdUtil.fastSimpleUUID();
        // 配置有效期 5 分钟
        redisCache.setCacheObject(CommonConst.QR_CODE_KEY.concat(uuid), null, 5, TimeUnit.MINUTES);
        // 生成二维码并返回
        QrConfig config = new QrConfig(300, 300);
        String qrCode = QrCodeUtil.generateAsBase64(uuid,config,"png");
        Map<String,String> qrMap = new HashMap<>();
        qrMap.put("uuid",uuid);
        qrMap.put("img",qrCode);
        return ResponseDTO.succData(
                qrMap
        );
    }
}
